<?php 

namespace Weixin\Controller;
use Common\Controller\WeixinBase;
use Common\Model\Model;

class TeachController extends WeixinbaseController
{
    
    public function index()
    {
        $teach = D('Weixin/Teach');
        
        $catid = I('get.catid');
        $catid = intval($catid) ? $catid : 0;
        $this->assign('catid', $catid);
        
        $count = $teach->getTeachCount($catid);
        $page = $this->Pages($count, 5);
        
        $list = $teach->getTeachList($catid, $page->firstRow, $page->listRows);
        if($_GET['code'] == 'getPage')
        {
            if(count($list) > 0)
            {
                $this->success($list);
            }
            $this->error('fail');
        }
        
        $categroy = $teach->getTeachCategory();
        $this->assign('category', $categroy);
        
        $ad = $teach->getTeachAd(5);
        $this->assign('ad', $ad);
//         echo "<pre>";
//         var_dump($list);
        
        $this->assign('list', $list);
        $this->display();
    }
    
    public function detail()
    {
        $teachid = I('get.teachid');
        if(!intval($teachid))
        {
            header('Location:'.WEB_URL.U('teach/index'));
	        exit;
        }
        
        $teach = D('Weixin/Teach');
        $data = $teach->getTeachInfo($teachid);
        
        $this->assign('info', $data);
        
        $where['infoid']=$teachid;
        $where['type']=7;
        $where['parent_id'] = 0;
        $count = M('UserMessage')->where($where)->count();
        // $limitRows = 5; // 设置每页记录数
        $limitRows = C('YF_PAGE');
        $page = $this->ajaxpage($count,$limitRows,'#msg','ajax_div','ajax_page_div');
        
        $Model=new Model();
        $message=$Model
        ->table(array(
            C('DB_PREFIX').'user_message'=>'M',
            C('DB_PREFIX').'user'=>'U',
        )
        )
        ->where('M.userid=U.userid and M.status=true and M.infoid='.$teachid.' and M.type=7 and M.parent_id=0')//yf 0912
        ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid,U.ifrz')
        ->order('M.create_time DESC')
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
        
        foreach($message as $k=>$v){
            $child_temp = array();
            $child_temp = $Model->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
        
            $message[$k]['childlist'] = array();
            if($child_temp)
            {
                foreach($child_temp as $yk => $yv)
                {
                    if($yv['userid'] == 0)
                    {
                        $child_temp[$yk]['nickname'] = "烘焙圈";
                        $child_temp[$yk]['openuid'] = 0;
                    }
                    else
                    {
                        $yf_user = array();
                        $yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
                        $child_temp[$yk]['nickname'] = $yf_user['nickname'];
                        $child_temp[$yk]['openuid'] = $yf_user['openuid'];
                        $child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
                        $child_temp[$yk]['ucity'] = $yf_user['ucity'];
                        $child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
                        $child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
                    }
                    if($yv['touserid'] != 0)
                    {
                        $child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
                    }
                    else
                    {
                        $child_temp[$yk]['touser']['nickname'] = "烘焙圈";
                        $child_temp[$yk]['touser']['openuid'] = 0;
                    }
                }
                $message[$k]['childlist'] = $child_temp;
            }
            $message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
            //$message[$k]['tempname'] = $v['nickname'];
            $message[$k]['touser'] = array();
            if(!empty($v['touserid']))
            {
                $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
            }
        }
        $this->assign('type', 7);
        $this->assign('msglist', $message);
        
        $val['viewnum'] = array('exp', "viewnum+1");
        M('Teachlist')->where(array('teachid' => $teachid))->save($val);
        
        $share['shareUrl'] = WEB_URL.$_SERVER['REQUEST_URI'];
        $share['shareTitle'] = $data['title'];
        $share['sharePic'] = $data['image'][0];
        $share['shareDesc'] = $data['title'];
        $this->assign('share', $share);
        $this->getJsdk();
        
        $this->display();
    }
    
    public function add()
    {
        if($_POST)
        {
            $post = I('post.');
            if(empty($post['title']))
            {
                $this->error('请输入专题名称');
            }
            $ifds = M('User')->where(array('userid' => $this->userid))->getField('ifds');
            if($post['type'] == 2 && $ifds == 0)
            {
                $this->error('你没有权限发布作品文章');
            }
            if(!$post['type'])
            {
                $this->error('请选择类型');
            }
            if(count($post['message']) == 0)
            {
                $this->error('至少要输入一段文字或者是上传一张图片');
            }
            if(!$post['message'][0])
            {
                $this->error('至少要输入一段文字或者是上传一张图片');
            }
            $post['content'] = '';
            $ifimg = 0;
            foreach($post['message'] as $k => $v)
            {
                if($v)
                {
                    if($post['msgtype'][$k] == 'text')
                    {
                        $post['content'] .= '<p>'.$v.'</p>';
                    }
                    if($post['msgtype'][$k] == 'pic')
                    {
                        $ifimg = 1;
                        $img = $this->getPic(array('0' => $v));
                        $tempImg = explode('/', $img['image']);
                        $tempImg[count($tempImg) - 1] = str_replace('yf_', '', $tempImg[count($tempImg) - 1]);
                        $post['content'] .= '<p><img src="'.implode('/', $tempImg).'" alt="" title=""></p>';
                    }
                }
            }
            unset($post['message']);unset($post['msgtype']);
            $post['create_time'] = time();
            $post['ifimg'] = $ifimg;
            $post['userid'] = $this->userid;
            $post['status'] = 1;
            $post['iflight'] = 0;
            $post['viewnum'] = 0;
            $post['msgnum'] = 0;
            $post['ifds'] = $ifds;
            $post['content'] = htmlspecialchars($post['content']);
            $id = M('Teachlist')->add($post);
            if($id)
            {
                $data['upoint'] = array('exp', "upoint+10");
                M('User')->where(array('userid' => $this->userid))->save($data);
                $this->success("发布成功", U('teach/index'));
            }
            else
            {
                $this->error("发布失败");
            }
        }
        $this->isLogin();
        $category = D('Weixin/Teach')->getTeachCategory();
        $this->assign('category', $category);
        $this->getJsdk();
        $this->display();
    }
    
}